package controllers

import (
	"doc-assistant/cache"
	"doc-assistant/models"
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
	"time"
)

type UserController struct {
	beego.Controller
}
type Res struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *UserController) Login() {
	username := c.GetString("username")
	password := c.GetString("password")
	if username == "" || password == "" {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "不为空",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	user := models.User{
		Username: username,
		Password: password,
	}
	err := models.DB.Debug().Where("username=?", username).Limit(1).First(&user).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "已存在",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	key := fmt.Sprintf("user_login_%d", models.User{}.ID)
	Count, _ := cache.Rdb.Get(cache.Ctx, key).Int()
	if Count >= 3 {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "错误三次拦截",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	if user.Password != password {
		cache.Rdb.Incr(cache.Ctx, key)
		if Count == 2 {
			cache.Rdb.Expire(cache.Ctx, key, time.Hour*2).Result()
		}
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "登录失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
}
func (c *UserController) Add() {
	id, _ := c.GetInt("id")
	name := c.GetString("name")
	tel := c.GetString("tel")
	hospital := c.GetString("hospital")
	from := c.GetString("from")
	cut := c.GetString("cut")
	doc := models.Doc{
		Id:       id,
		Name:     name,
		Tel:      tel,
		Hospital: hospital,
		From:     from,
		Cut:      cut,
	}
	err := models.DB.Create(&doc).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "成功",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
}
func (c *UserController) Del() {
	id, _ := c.GetInt("id")
	err := models.DB.Delete(&models.Doc{}, id).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "成功",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
}
func (c *UserController) Up() {
	id, _ := c.GetInt("id")
	name := c.GetString("name")
	tel := c.GetString("tel")
	hospital := c.GetString("hospital")
	from := c.GetString("from")
	cut := c.GetString("cut")
	doc := models.Doc{
		Id:       id,
		Name:     name,
		Tel:      tel,
		Hospital: hospital,
		From:     from,
		Cut:      cut,
	}
	err := models.DB.Where("id=?", id).Updates(&doc).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "成功",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
}
func (c *UserController) List() {
	var doc []models.Doc
	err := models.DB.Find(&doc).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "成功",
			Data: doc,
		}
		c.ServeJSON()
		return
	}
}
